package com.ytjj.qmyx.users.controller.taoxingou;

import com.ytjj.common.api.CommonResult;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.users.model.request.LoginRequest;
import com.ytjj.qmyx.users.service.LoginService;
import com.ytjj.qmyx.users.utils.IPUtil;
import com.ytjj.qmyx.users.utils.SmsToolsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/h5Login")
public class H5LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * h5 注册登陆
     *
     * @param loginRequest
     * @return
     */
    @PostMapping("/register")
    public CommonResult register(@RequestBody LoginRequest loginRequest) {
        loginRequest.setIp(IPUtil.getIpAddress(TokenUtil.getRequest()));
        loginRequest.setChannel(TokenUtil.getRequest().getHeader("channel"));
        return CommonResult.success(loginService.h5Register(loginRequest));
    }

    /**
     * 解析手机号
     * @param serialNumber
     * @return
     */
    @GetMapping("/analysisSmsCode")
    public CommonResult analysisSmsCode(@RequestParam(value="serialNumber") String serialNumber) {
        if (StringUtils.isEmpty(serialNumber)) {
            throw new ApiException("参数错误！");
        }
        String phone = SmsToolsUtil.resolutionSmsToolsPhone(serialNumber);
        return CommonResult.success(phone);
    }
}
